package com.liang.maogouyuyin.ui.adapter

import android.content.Context
import android.content.Intent
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.liang.maogouyuyin.AppConst
import com.liang.maogouyuyin.R
import com.liang.maogouyuyin.bean.AnomalyBean
import com.liang.maogouyuyin.bean.CasebookBean
import com.liang.maogouyuyin.bean.ExpenditureBean
import com.liang.maogouyuyin.bean.WeightBean
import com.liang.maogouyuyin.databinding.ItemAnomalyBinding
import com.liang.maogouyuyin.databinding.ItemBillBinding
import com.liang.maogouyuyin.databinding.ItemCasebookBinding
import com.liang.maogouyuyin.databinding.ItemWeightBinding
import com.liang.maogouyuyin.ui.activity.AddDetailActivity
import com.liang.maogouyuyin.utils.DateUtil
import com.bumptech.glide.Glide
import java.io.File

class RecordDetailAdapter(val context: Context): RecyclerView.Adapter<RecyclerView.ViewHolder>() {

    private var itemType: Int = 0

    private lateinit var list: ArrayList<out Any>

    override fun getItemViewType(position: Int): Int {
        return itemType
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        return when(viewType) {
            1 -> WeightViewHolder(ItemWeightBinding.inflate(LayoutInflater.from(context), parent, false))
            2 -> AnomalyViewHolder(ItemAnomalyBinding.inflate(LayoutInflater.from(context), parent, false))
            3 -> CasebookViewHolder(ItemCasebookBinding.inflate(LayoutInflater.from(context), parent, false))
            else -> BillViewHolder(ItemBillBinding.inflate(LayoutInflater.from(context), parent, false))
        }
    }

    override fun getItemCount(): Int {
        return list.size
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        when (itemType) {
            0 -> {
                val itemHolder = holder as BillViewHolder
                val bean = list[position] as ExpenditureBean
                itemHolder.itemDate.text = DateUtil.formatTimeStamp(bean.date)
                itemHolder.itemBalance.text = String.format(context.getString(R.string.record_detail_bill_sum), bean.money)
                itemHolder.itemContent.text = bean.content
                itemHolder.itemView.setOnClickListener {
                    AppConst.bean = bean
                    startAddDetailActivity(itemType)
                }
            }
            1 -> {
                val itemHolder = holder as WeightViewHolder
                val bean = list[position] as WeightBean
                itemHolder.itemAge.text = bean.name
                itemHolder.itemWeight.text = String.format(context.getString(R.string.record_detail_weight_sum), bean.weight)
                itemHolder.itemDate.text = DateUtil.formatTimeStamp(bean.date)
                itemHolder.itemView.setOnClickListener {
                    AppConst.bean = bean
                    startAddDetailActivity(itemType)
                }
            }
            2 -> {
                val itemHolder = holder as AnomalyViewHolder
                val bean = list[position] as AnomalyBean
                itemHolder.itemDate.text = DateUtil.formatTimeStamp(bean.date)
                itemHolder.itemContent.text = bean.content
                itemHolder.itemName.text = bean.anomalyName
                if (bean.image.isNotEmpty()) {
                    itemHolder.itemImage.visibility = View.VISIBLE
                    Glide.with(context).load(File(bean.image)).into(itemHolder.itemImage)
                } else
                    itemHolder.itemImage.visibility = View.GONE
                itemHolder.itemView.setOnClickListener {
                    AppConst.bean = bean
                    startAddDetailActivity(itemType)
                }
            }
            3 -> {
                val itemHolder = holder as CasebookViewHolder
                val bean = list[position] as CasebookBean
                itemHolder.itemDate.text = DateUtil.formatTimeStamp(bean.date)
                itemHolder.itemHospital.text = bean.hospital
                itemHolder.itemContent.text = bean.content
                itemHolder.itemName.text = bean.casebook
                if (bean.image.isNotEmpty()) {
                    itemHolder.itemImage.visibility = View.VISIBLE
                    Glide.with(context).load(File(bean.image)).into(itemHolder.itemImage)
                } else
                    itemHolder.itemImage.visibility = View.GONE
                itemHolder.itemView.setOnClickListener {
                    AppConst.bean = bean
                    startAddDetailActivity(itemType)
                }
            }
        }
    }

    private fun startAddDetailActivity(type: Int) {
        val intent = Intent(context, AddDetailActivity::class.java)
        intent.putExtra("type", type)
        context.startActivity(intent)
    }

    fun setType(type: Int) {
        itemType = type
    }

    fun setList(list: ArrayList<out Any>) {
        this.list = list
    }

    class BillViewHolder(binding: ItemBillBinding): RecyclerView.ViewHolder(binding.root) {
        val itemDate = binding.itemBillDate
        val itemContent = binding.itemBillContent
        val itemBalance = binding.itemBillBalance
    }

    class WeightViewHolder(binding: ItemWeightBinding): RecyclerView.ViewHolder(binding.root) {
        val itemAge = binding.itemWeightPetAge
        val itemWeight = binding.itemWeight
        val itemDate = binding.itemWeightDate
    }

    class AnomalyViewHolder(binding: ItemAnomalyBinding): RecyclerView.ViewHolder(binding.root) {
        val itemName = binding.itemAnomalyName
        val itemImage = binding.itemAnomalyImage
        val itemContent = binding.itemAnomalyContent
        val itemDate = binding.itemAnomalyDate
    }

    class CasebookViewHolder(binding: ItemCasebookBinding): RecyclerView.ViewHolder(binding.root) {
        val itemName = binding.itemCasebookName
        val itemHospital = binding.itemCasebookHospital
        val itemContent = binding.itemCasebookContent
        val itemImage = binding.itemCasebookImage
        val itemDate = binding.itemCasebookDate
    }
}